package com.service.purchase;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.model.Global;
import com.controller.purchase.NeedController;
import com.dao.purchase.NeedMapper;
import com.dao.purchase.NeedMmMapper;
import com.dao.purchase.PlanMapper;
import com.dao.purchase.PlanMmMapper;
import com.model.purchase.PurchaseInquiry;
import com.model.purchase.PurchaseMm;
import com.model.purchase.PurchaseNeed;
import com.model.purchase.PurchaseNeedMm;
import com.model.purchase.PurchaseNeedVO;
import com.model.purchase.PurchasePlan;
import com.model.purchase.PurchasePlanMm;

/**
 * 需求管理
 * 
 * @author chenjun
 *
 */
@Service
public class NeedService {

	static final Logger logger = LoggerFactory.getLogger(NeedService.class);

	@Autowired
	NeedMapper needMapper;
	@Autowired
	NeedMmMapper needMmMapper;
	@Autowired
	PlanMapper planMapper;
	@Autowired
	PlanMmMapper planMmMapper;

	/**
	 * 需求计划列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectNeedList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		List<PurchaseNeed> list = needMapper.selectList(queryMap);
		int count = needMapper.selectListCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("purchaseNeedMapper:{}", list);

		return httpResult;
	}

	/**
	 * 需求明细列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectNeedMmList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		List<PurchaseNeedMm> list = needMmMapper.selectList(queryMap);
		int count = needMmMapper.selectListCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("selectNeedMmList:{}", list);

		return httpResult;
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	@Transactional
	public HttpResult addNeed(PurchaseNeedVO purchaseNeedVO) {
		if (purchaseNeedVO == null || purchaseNeedVO.getPurchaseNeedMms() == null) {
			return HttpResult.error("参数为空");
		}
		HttpResult httpResult = new HttpResult();

		purchaseNeedVO.setNedId(purchaseNeedVO.getNedCode());

		// 需求计划
		purchaseNeedVO.setStatus(Global.YES + "");
		if (needMapper.update(purchaseNeedVO) == 0) {
			needMapper.insert(purchaseNeedVO);
		}
		for (PurchaseNeedMm purchaseNeedMm : purchaseNeedVO.getPurchaseNeedMms()) {
			purchaseNeedMm.setNedId(purchaseNeedVO.getNedId());
			purchaseNeedMm.setNedName(purchaseNeedVO.getNedName());
			purchaseNeedMm.setNedCode(purchaseNeedVO.getNedCode());
			if (needMmMapper.update(purchaseNeedMm) == 0) {
				needMmMapper.insert(purchaseNeedMm);
			}
		}
		// 采购计划
		PurchasePlan purchasePlan = initPlanBean(purchaseNeedVO);
		purchasePlan.setStatus(Global.NO + "");
		if (planMapper.update(purchasePlan) == 0) {
			planMapper.insert(purchasePlan);
		}
		for (PurchaseNeedMm purchaseNeedMm : purchaseNeedVO.getPurchaseNeedMms()) {
			PurchasePlanMm purchasePlanMm = initPlanMmBean(purchasePlan, purchaseNeedMm);
			purchasePlanMm.setPlanCode(purchasePlan.getPlanCode());
			purchasePlanMm.setPlanName(purchasePlan.getPlanName());
			if (planMmMapper.update(purchasePlanMm) == 0) {
				planMmMapper.insert(purchasePlanMm);
			}
		}

		return httpResult;
	}

	// 根据需求计划生成采购计划
	private PurchasePlan initPlanBean(PurchaseNeedVO purchaseNeedVO) {
		PurchasePlan purchasePlan = new PurchasePlan();
		purchasePlan.setPlanId(purchaseNeedVO.getNedCode());
		purchasePlan.setPlanCode(purchaseNeedVO.getNedCode());
		purchasePlan.setPlanPurType(purchaseNeedVO.getNedPurType());

		purchasePlan.setPlanName(purchaseNeedVO.getNedName());
		purchasePlan.setPlanCategory(purchaseNeedVO.getNedCategory());
		purchasePlan.setPlanType(purchaseNeedVO.getNedType());
		purchasePlan.setPlanMmType(purchaseNeedVO.getNedMmType());
		purchasePlan.setPlanProCode(purchaseNeedVO.getNedProCode());
		purchasePlan.setPlanPurType(purchaseNeedVO.getNedPurType());
		purchasePlan.setPlanDeptCode(purchaseNeedVO.getNedDeptCode());
		purchasePlan.setPlanDeptName(purchaseNeedVO.getNedDeptName());
		purchasePlan.setPlanIsNeed(purchaseNeedVO.getNedIsUrgent());
		purchasePlan.setPlanProName(purchaseNeedVO.getNedProName());

		return purchasePlan;
	}

	// 根据需求计划明细生成采购计划
	private PurchasePlanMm initPlanMmBean(PurchasePlan purchasePlan, PurchaseNeedMm purchaseNeedMm) {
		PurchasePlanMm purchasePlanMm = new PurchasePlanMm();
		purchasePlanMm.setPlanId(purchasePlan.getPlanId());
		purchasePlanMm.setMmId(purchaseNeedMm.getMmId());
		purchasePlanMm.setMmCode(purchaseNeedMm.getNedMmCode());
		purchasePlanMm.setMmUnit(purchaseNeedMm.getNedMmUnit());
		purchasePlanMm.setPlanNeedDate(purchaseNeedMm.getNedNeedDate());
		purchasePlanMm.setPlanNeedNum(purchaseNeedMm.getNedPlanNum());
		purchasePlanMm.setPlanPurNum(purchaseNeedMm.getNedPlanNum());
		return purchasePlanMm;
	}

	/**
	 * 导出
	 * 
	 * @param queryMap
	 * @param response
	 */
	public void exportNeed(Map<String, Object> queryMap, HttpServletResponse response) {
		List<PurchaseNeed> list = needMapper.selectList(queryMap);

		logger.info("exportProgram:{}", list);
		try {
			EasyExcel.write(response.getOutputStream(), PurchaseNeed.class).sheet("模板").doWrite(list);
		} catch (IOException e) {
			logger.error("导出错误:{}", e.getMessage());
		}

	}
}
